package com.chuanyang.driverh.mainhome.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.response2.join.ResultDriverApplyList;
import com.chuanyang.driverh.response2.join.ResultFleetJoinTop;
import com.chuanyang.driverh.response2.join.ResultSearchList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class FleetSearchAdapter extends BaseListAdapter<ResultSearchList.DataBeanX.DataBean> {
    private Context mContext;

    public FleetSearchAdapter(Context context) {
        super(context);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.mContext = context;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.fleet_search_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_join = (ImageView) convertView.findViewById(R.id.iv_join);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);

            viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.tv_car_num = (TextView) convertView.findViewById(R.id.tv_car_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ResultSearchList.DataBeanX.DataBean dataBean = getItem(position);

        viewHolder.tv_name.setText(dataBean.getFleetName());
//        viewHolder.tv_phone.setText(dataBean.get);
//        viewHolder.tv_car_num.setText(dataBean.get);

        viewHolder.iv_join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//加盟
                addBusinessRelation();
            }
        });


        return convertView;
    }

    class ViewHolder {
        ImageView iv_join;
        TextView tv_name;
        TextView tv_phone;
        TextView tv_car_num;
    }
    //加盟的接口
    private void addBusinessRelation() {
        DialogUtil.showLoadingDialog(mContext,"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("initiator", "");
        params.put("pendi", "");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("fleetId", SharedPrefUtil.getFleetId());

        MyHttpUtil.addBusinessRelation(mContext,params, new MyCallback<Result>(mContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(mContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {

            }
        });
    }
}
